// import state from "./state"

export default {
    addItem (state, data) {
        const hasItem = state.cartItemList.some(item => item.sku_id === data.sku_id)
        if (hasItem) {
            state.cartItemList.map(item => {
                if (item.sku_id === data.sku_id) {
                    item.count += data.count
                }
                return item
            })
        } else {
            state.cartItemList.push(data)
        }
        // console.log(state.cartItemList)
    },
    changeChecked (state, { id }) {
        state.cartItemList.map(item => {
            if (item.sku_id === id) {
                item.checked = !item.checked
            }
            return item
        })
    },
    changeAllChecked (state) {
        const allChecked = state.cartItemList.every(item => item.checked)
        state.cartItemList.map(item => {
            if (allChecked) {
                item.checked = false
            } else {
                item.checked = true
            }
            return item
        })
    },
    allNoChecked (state) {
        state.cartItemList.map(item => {
            item.checked = false
            return item
        })
    },
    plusItemCount (state, { id }) {
        state.cartItemList.map(item => {
            if (item.sku_id === id) {
                item.count++
            }
            return item
        })
    },
    minusItemCount (state, { id }) {
        state.cartItemList.map(item => {
            if (item.sku_id === id) {
                item.count--
            }
            return item
        })
    },
    delateItem (state) {
        state.cartItemList = state.cartItemList.filter(item => !item.checked)
    },
    setCart (state, data) {
        state.cartItemList = data
        localStorage.setItem('cart', JSON.stringify(data))
    }
}
